package com.newbee.fragement;

import java.util.ArrayList;
import java.util.List;

import android.R.string;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.activity.MultChatChooseFriend;
import com.newbee.adapter.QunZuAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.fragement.multchat.MultChatFragement;
import com.newbee.http.ServerHttpUtil;
import com.newbee.listbean.QunZuInfoBean;
import com.newbee.multchat.MultChatActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.smack.SmackClient;
import com.newbee.util.MD5Util;
import com.readystatesoftware.viewbadger.BadgeView;

public class QunzuFragment extends Fragment implements OnRefrushActivity,
		OnItemClickListener
{

	private ListView mListView;

	private List<QunZuInfoBean> mlist;

	private QunZuAdapter madapter;

	private View view;

	private TextView emptyTextView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{

		view = inflater.inflate(R.layout.fragment_qunzu, container, false);
		emptyTextView = (TextView) view.findViewById(R.id.qunzulist_empty);
		MainServiceTask mst = new MainServiceTask(TaskType.GET_QUNZU, null,
				QunzuFragment.class.getName());
		MainService.addActivityMap(QunzuFragment.class.getName(), this);
		MainService.addTaskQueue(mst);
		MainService.addSpecialActivity(QunzuFragment.class.getName(), this);
		mListView = (ListView) view.findViewById(R.id.qunzulist);
		mListView.setOnItemClickListener(this);
		return view;
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_QUNZU:
		{

			this.mlist = (List<QunZuInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");
				mListView.setEmptyView(emptyTextView);

			}
			else if (madapter == null)
			{
				this.madapter = new QunZuAdapter(this.getActivity(), mlist);
				this.mListView.setAdapter(this.madapter);
			}
			else
			{
				this.madapter.setList(mlist);
				this.madapter.notifyDataSetChanged();
			}
			break;
		}
		case TaskType.MULT_CHAT_MSG:
		{

			// String roomName = (String) paras[0];
			//
			// updateListview(roomName);
			break;
		}

		case TaskType.CREATE_MULT_CHAT:
		{

			String qunzuName = (String) paras[0];

			List<String> userlist = (List<String>) paras[1];

			List<String> rl = new ArrayList<String>();
			for (String s : userlist)
			{
				rl.add(s.split("@")[0]);

			}
			mlist.add(new QunZuInfoBean(qunzuName, rl));
			if (this.madapter == null)
			{

				this.madapter = new QunZuAdapter(this.getActivity(), mlist);
				this.mListView.setAdapter(madapter);
			}
			else
			{
				this.madapter.notifyDataSetChanged();
			}

			break;
		}

		case TaskType.ACCEPT_MULT:
		{

		}
		default:
			break;
		}
		return null;

	}

	// public void updateListview(String roomName)
	// {
	//
	// int length = mlist.size();
	//
	// QunZuInfoBean bean;
	//
	// for (int i = 0; i < length; i++)
	// {
	// bean = mlist.get(i);
	//
	// if (roomName.startsWith(bean.getQunzuName()))
	// {
	// int mumb = bean.getTipnumb() + 1;
	// bean.setTipnumb(mumb);
	// break;
	// }
	//
	// }
	// this.madapter.setList(mlist);
	// this.madapter.notifyDataSetChanged();
	//
	// }

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3)
	{

		Log.d("WCH", "群组itme click");
		QunZuInfoBean bean = this.mlist.get(p);
		String qunzuName = bean.getQunzuName();
		// 要注意当前类为fragment而不是activity；
		Intent intent = new Intent(this.getActivity(), MultChatActivity.class);
		Bundle bundle = new Bundle();
		bundle.putCharSequence(MultChatFragement.CHAT_ROOM_MAME,
				MD5Util.multChatRoom(qunzuName));
		bundle.putStringArrayList(MultChatFragement.USERS_LIST,
				usersPauser(bean.getUserList()));

		bundle.putString(MultChatChooseFriend.QZ_NAME, qunzuName);

		intent.putExtras(bundle);
		this.getActivity().startActivity(intent);

	}

	private ArrayList<String> usersPauser(List<String> users)
	{

		ArrayList<String> list = new ArrayList<String>();
		for (String s : users)
		{

			list.add(s + "@" + SmackClient.SERVER_NAME);
		}

		return list;
	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(QunzuFragment.class.getName());
		super.onDestroy();
	}
}
